package cn.edu.neusoft.yuejian13.yuejian_13.model;


import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


import cn.edu.neusoft.yuejian13.yuejian_13.listener.List_TListener;
import cn.edu.neusoft.yuejian13.yuejian_13.listener.TListener;
import cn.edu.neusoft.yuejian13.yuejian_13.service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public class RetrofitBaseModel {
    Service service;
    Retrofit retrofit;
   private static final String BASE_URL = "http://10.0.2.2/yuejian10/public/index.php/json/";//主机地址
  // private static final String BASE_URL = "http://172.19.186.228:80/yuejian10/public/index.php/json/";//主机地址

    public RetrofitBaseModel() {
        this.retrofit = getRetrofit();
        this.service = getService();
    }



    public Retrofit getRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                //.addConverterFactory(ScalarsConverterFactory.create())
                .build();
        return retrofit;
    }


    public Service getService() {
        service = retrofit.create(Service.class);
        return service;
    }

    public <T> void callenqueue(Call<T> call, final TListener<T> tListener) {
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                if (response.body() != null && response.isSuccessful()) {
                    tListener.onResponse(response.body());
                } else {
                    tListener.onFail("Fail!");
                }
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                tListener.onFail(t.toString());
            }
        });
    }


    public <T> void callenqueue_str(Call<String> call, final TListener<T> tListener, final Class<T> cls) {
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.body() != null && response.isSuccessful()) {
                    JsonObject jsonObject ;
                    try {
                        jsonObject = new JsonParser().parse(response.body()).getAsJsonObject();
                        String err = jsonObject.get("error").getAsString();
                        String msg = jsonObject.get("msg").getAsString();

                        if (err == null || msg == null) {
                            tListener.onFail("parse json fail!"+err+"--"+msg);
                        } else if (err.equals("1")) {
                            tListener.onFail(msg);
                        } else if (err.equals("0")) {
                           // Gson gson = new GsonBuilder().setLenient().create();
                            T t = new Gson().fromJson(jsonObject.get("data").toString(), cls);
                            tListener.onResponse(t);
                        }
                    } catch (Exception e) {
                        Log.i("RESPONSE_ERROR", "" + e.getMessage());
                    }
                } else {
                    tListener.onFail("Fail!");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                tListener.onFail(t.toString());
            }
        });
    }

    public <T> void callenqueue_list(Call<List<T>> call, final List_TListener<T> list_tListener) {
        call.enqueue(new Callback<List<T>>() {
            @Override
            public void onResponse(Call<List<T>> call, Response<List<T>> response) {
                if (response.body() != null && response.isSuccessful()) {
                    list_tListener.onResponse(response.body());
                } else {
                    list_tListener.onFail("Fail!");
                }
            }

            @Override
            public void onFailure(Call<List<T>> call, Throwable t) {

            }
        });


    }

    public <T> void callenqueue_list_str(Call<String> call, final List_TListener<T> tListener, final Class<T> cls) {
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.body() != null && response.isSuccessful()) {
                    JsonObject jsonObject;
                    try {
                        jsonObject = new JsonParser().parse(response.body()).getAsJsonObject();
                        String err = jsonObject.get("error").getAsString();
                        String msg = jsonObject.get("msg").getAsString();

                        if (err == null || msg == null) {
                            tListener.onFail("parse json fail!");
                        } else if (err.equals("1")) {
                            tListener.onFail(msg);
                        } else if (err.equals("0")) {
                            Type type = new TypeToken<ArrayList<T>>(){}.getType();
                            Gson gson = new Gson();
                            ArrayList<T> list = new ArrayList<>();
                            Log.i("TYPE", type.toString());
                            Log.i("DATA", jsonObject.get("data").toString());
                            JsonArray cars = jsonObject.get("data").getAsJsonArray();
                            for (JsonElement jsonElement: cars) {
                                Log.i("js", jsonElement.toString());
                                list.add(gson.fromJson(jsonElement, cls));
                            }
//                            list = gson.fromJson(jsonObject.get("data").toString(), type);
                            tListener.onResponse(list);
                        }
                    } catch (Exception e) {
                        Log.i("RESPONSE_ERROR", "" + e.getMessage());
                    }
                } else {
                    tListener.onFail("Fail!");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                tListener.onFail(t.toString());
            }
        });
    }



}
